package com.woniuxy.myorders.service.impl;

import com.woniuxy.commons.entity.OrdersLog;
import com.woniuxy.commons.utils.GetIp;
import com.woniuxy.myorders.mapper.OrdersLogMapper;
import com.woniuxy.myorders.service.OrdersLogService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @author lhc
 * @create 2021-11-16 16:36
 */
@Service
public class OrdersLogServiceImpl implements OrdersLogService {
    @Resource
    private OrdersLogMapper ordersLogMapper;
    @Resource
    private RedisTemplate<String,Object> redisTemplate;

    /**
     * 添加订单日志
     * @param description 获取使用的方法
     */
    @Override
    public void addOrdersLog(String description) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String username = (String) redisTemplate.opsForValue().get("username");
        String ip = GetIp.getIpAddress(request);
        OrdersLog ordersLog =new OrdersLog();
        ordersLog.setContent(description);
        ordersLog.setHostip(ip);
        ordersLog.setUsername(username);
        ordersLog.setOperatortime(new Date());
        ordersLogMapper.addOrdersLog(ordersLog);
    }

    @Override
    public List<OrdersLog> getAllOrdersLog(Integer pageIndex, Integer pageSize) {
        return ordersLogMapper.getAllOrdersLog(pageIndex, pageSize);
    }

    @Override
    public void delOrdersLog(int logid) {
        ordersLogMapper.delOrdersLog(logid);
    }

    @Override
    public int selectOrdersLogCount() {
        return ordersLogMapper.selectOrdersLogCount();
    }
}
